54. Archive Module

Note

The below information is extensively based in information taken from the PowerShell® Notes for Professionals book. I plan to extend this information based on my day to day usage of the language.

  • CompressionLevel (Compress-Archive only): Set compression level to either Fastest, Optimal or NoCompression
  • Confirm: Prompts for confirmation before running
  • Force: Forces the command to run without confirmation
  • LiteralPath: Path that is used literally, no wildcards supported , use , to specify multiple paths
  • Path: Path that can contain wildcards, use , to specify multiple paths
  • Update: (Compress-Archive only) Update existing archive
  • WhatIf: Simulate the command

The Archive module Microsoft.PowerShell.Archive provides functions for storing files in ZIP archives (Compress-Archive) and extracting them (Expand-Archive). This module is available in PowerShell 5.0 and above.

In earlier versions of PowerShell the Community Extensions or .NET System.IO.Compression.FileSystem could be used.

54.1: Compress-Archive with wildcard

1
Compress-Archive -Path C:\Documents\* - CompressionLevel Optimal -DestinationPath C:\Archives\Documents.zip

This command:

  • Compresses all files in C:\Documents
  • Uses Optimal compression
  • Save the resulting archive in C:\Archives\Documents.zip
    • DestinationPath will add .zipif not present.
    • -LiteralPath_ can be used if you require naming it without .zip.

54.2: Update existing ZIP with Compress-Archive

1
Compress-Archive -Path C:\Documents\* - Update -DestinationPath C:\Archives\Documents.zip
  • this will add or replace all files Documents.zip with the new ones from C:\Documents

54.3: Extract a Zip with Expand-Archive

1
Expand-Archive -Path C:\Archives\Documents.zip -DestinationPath C:\Documents
  • this will extract all files from Documents.zip into the folder C:\Documents